package com.wangxinyuchuan.demo.service.impl;

import com.wangxinyuchuan.demo.constant.DiscountVolumeEnum;
import com.wangxinyuchuan.demo.model.DiscountVolume;
import com.wangxinyuchuan.demo.service.DiscountVolumeQueryService;
import com.wangxinyuchuan.demo.service.DiscountVolumeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Service
public class DisCountVolumeQueryServiceImpl implements DiscountVolumeQueryService {

    @Autowired
    private DiscountVolumeTypeService discountVolumeTypeService;

    private final Map<String, Function<String, DiscountVolume>> typeMap = new HashMap<>();

    /**
     * 需要增加优惠卷类型的时候只需要在初始化方法增加就行
     */
    @PostConstruct
    public void initTypeMap() {
        typeMap.put(DiscountVolumeEnum.RED_PAPER.getType(), resourceId -> discountVolumeTypeService.redPaper(resourceId));
        typeMap.put(DiscountVolumeEnum.SHOPPING.getType(), resourceId -> discountVolumeTypeService.shopping(resourceId));
        typeMap.put(DiscountVolumeEnum.QQ_VIP.getType(), resourceId -> discountVolumeTypeService.QQVip(resourceId));
    }


    @Override
    public DiscountVolume getResult(String resourceType, String resourceId) {
        Function<String, DiscountVolume> result = typeMap.get(resourceType);
        if (null != result) {
            return result.apply(resourceId);
        }
        return null;
    }
}
